package cqcvc.edu.cn.dao;


import cqcvc.edu.cn.base.BaseDao;
import cqcvc.edu.cn.entity.ServiceType;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ServiceTypeDao {

    public List<ServiceType> findTypeAll(){
        Connection connection = BaseDao.getConn();

        try{
            Statement state = connection.createStatement();//容器
            String sql = "select * from service_type";
            ResultSet resultSet = state.executeQuery(sql);
            return getTypeList(resultSet);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public ServiceType findByTypeId(String id){
        Connection connection = BaseDao.getConn();

        try{
            Statement state=connection.createStatement();//容器
            String sql = "select * from service_type where ID = " + id ;
            ResultSet resultSet = state.executeQuery(sql);
            List<ServiceType> his= getTypeList(resultSet);
            if(his.size()>0)
            {
                return his.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return null;
    }

    private List<ServiceType> getTypeList(ResultSet resultSet){
        try {
            List<ServiceType> list = new ArrayList<>();

            while (resultSet.next()){
                ServiceType serviceType = new ServiceType();

                serviceType.setId(resultSet.getLong(1));
                serviceType.setTypeTitle(resultSet.getString(2));
                serviceType.setCreateDate(resultSet.getDate(3));
                serviceType.setShowOrder(resultSet.getInt(4));
                serviceType.setIfShow(resultSet.getString(5));
                list.add(serviceType);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
